package com.ssy.lingxi.search.controller.pc;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.constant.member.MemberTypeEnum;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.search.api.model.request.CommodityCollectBatchRequest;
import com.ssy.lingxi.search.api.model.request.CommodityCollectRequest;
import com.ssy.lingxi.search.api.model.request.SimpleIdRequest;
import com.ssy.lingxi.search.api.model.response.CollectResponse;
import com.ssy.lingxi.search.api.model.response.CommoditySearchResponse;
import com.ssy.lingxi.search.entity.Commodity;
import com.ssy.lingxi.search.entity.CommodityCollect;
import com.ssy.lingxi.search.entity.CommodityShop;
import com.ssy.lingxi.search.entity.CommoditySoldCount;
import com.ssy.lingxi.search.entity.channel.ChannelCommodityMember;
import com.ssy.lingxi.search.entity.channel.CommodityCollectChannel;
import com.ssy.lingxi.search.model.response.CommodityCollectResponse;
import com.ssy.lingxi.search.repository.CommodityRepository;
import com.ssy.lingxi.search.service.ICommodityCollectChannelService;
import com.ssy.lingxi.search.service.ICommodityCollectService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @menu: 商城--商品收藏管理
 * 商品收藏管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/9/2
 */
@RestController
@RequestMapping("/search/shop/commodityCollect")
public class CommodityCollectController extends BaseController {

    @Resource
    private ICommodityCollectService commodityCollectService;

    @Resource
    private ICommodityCollectChannelService commodityCollectChannelService;

    @Resource
    private CommodityRepository commodityRepository;

    /**
     * 查询商品是否已经收藏
     * @param commodityId 商品id
     */
    @RequestMapping(value = "/getCommodityCollect", method = RequestMethod.GET)
    public Wrapper<CollectResponse> getCommodityCollect(@RequestParam Long commodityId) {
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityCollectService.getCommodityCollectByCommodityId(commodityId, sysUser, shopId));
    }

    /**
     * 查询商品收藏列表
     * @return
     */
    @RequestMapping(value = "/getCommodityCollectList", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityCollectResponse>> getCommodityCollectList(PageVO pageVO) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Integer memberType = sysUser.getMemberType();
        if(MemberTypeEnum.MERCHANT.getCode().equals(memberType) || MemberTypeEnum.MERCHANT_PERSONAL.getCode().equals(memberType)){
            Page<CommodityCollect> result = commodityCollectService.getCommodityCollectList(sysUser, pageVO);
            List<CommodityCollectResponse> resultList = result.getContent().stream().map(commodityCollect -> {
                CommodityCollectResponse commodityCollectResponse = new CommodityCollectResponse();
                commodityCollectResponse.setIsPublish(false);
                Long commodityId = commodityCollect.getCommodityId();
                Long commodityCollectId = commodityCollect.getId();
                Long createTime = commodityCollect.getCreateTime();
                Long shopId = commodityCollect.getShopId();
                commodityCollectResponse.setId(commodityCollectId);
                commodityCollectResponse.setShopId(shopId);
                commodityCollectResponse.setCreateTime(createTime);
                Commodity commodity = commodityRepository.findById(commodityId).orElse(null);
                if(commodity != null){
                    CommoditySearchResponse commoditySearchResponse = new CommoditySearchResponse();
                    BeanUtil.copyProperties(commodity, commoditySearchResponse);
                    commodityCollectResponse.setCommodity(commoditySearchResponse);

                    //不同商城显示不同的已售数量
                    List<CommoditySoldCount> commoditySoldCountList = commodity.getCommoditySoldCountList();
                    if(commoditySoldCountList != null && commoditySoldCountList.size() > 0){
                        commoditySoldCountList.forEach(commoditySoldCount -> {
                            Long commoditySoldCountShopId = commoditySoldCount.getShopId();
                            double sold = commoditySoldCount.getSold();
                            if(commoditySoldCountShopId.equals(shopId)){
                                commoditySearchResponse.setSold(sold);
                            }
                        });
                    }

                    //判断会员商品是否上架
                    List<CommodityShop> commodityShopList = commodity.getCommodityShopList();
                    if(commodityShopList != null && commodityShopList.size() > 0){
                        List<Long> shopIdList = commodityShopList.stream().map(CommodityShop::getShopId).collect(Collectors.toList());
                        if(shopIdList.contains(shopId)){
                            commodityCollectResponse.setIsPublish(true);
                        }
                    }
                }else{
                    return null;
                }
                return commodityCollectResponse;
            }).filter(Objects::nonNull).collect(Collectors.toList());
            return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
        }else{
            Page<CommodityCollectChannel> result = commodityCollectChannelService.getCommodityCollectChannelList(sysUser, pageVO);
            List<CommodityCollectResponse> resultList = result.getContent().stream().map(commodityCollectChannel -> {
                Long channelCommodityId = commodityCollectChannel.getChannelCommodityId();
                Long commodityCollectId = commodityCollectChannel.getId();
                Long createTime = commodityCollectChannel.getCreateTime();
                Long shopId = commodityCollectChannel.getShopId();
                Long channelMemberId = commodityCollectChannel.getChannelMemberId();
                Long channelMemberRoleId = commodityCollectChannel.getChannelMemberRoleId();

                CommodityCollectResponse commodityCollectResponse = new CommodityCollectResponse();
                commodityCollectResponse.setIsPublish(false);
                commodityCollectResponse.setId(commodityCollectId);
                commodityCollectResponse.setShopId(shopId);
                commodityCollectResponse.setCreateTime(createTime);
                commodityCollectResponse.setChannelMemberId(channelMemberId);
                commodityCollectResponse.setChannelMemberRoleId(channelMemberRoleId);

                Commodity commodity = commodityRepository.findFirstByMemberListChannelCommodityMemberId(channelCommodityId);
                if(commodity != null){
                    CommoditySearchResponse commoditySearchResponse = new CommoditySearchResponse();
                    BeanUtil.copyProperties(commodity, commoditySearchResponse);
                    commodityCollectResponse.setCommodity(commoditySearchResponse);
                    List<ChannelCommodityMember> memberList = commodity.getMemberList();
                    if(memberList != null && memberList.size() > 0){
                        memberList.forEach(channelCommodityMember -> {
                            Long channelCommodityMemberId = channelCommodityMember.getChannelMemberId();
                            Long channelCommodityMemberRoleId = channelCommodityMember.getChannelMemberRoleId();
                            if(channelCommodityMemberId.equals(channelMemberId) && channelCommodityMemberRoleId.equals(channelMemberRoleId)){
                                List<CommodityShop> shopList = channelCommodityMember.getShopList();
                                if(shopList != null && shopList.size() > 0){
                                    List<Long> shopIdList = shopList.stream().map(CommodityShop::getShopId).collect(Collectors.toList());
                                    if(shopIdList.contains(shopId)){
                                        commodityCollectResponse.setIsPublish(true);
                                    }
                                }
                            }
                        });
                    }
                }else{
                    return null;
                }
                return commodityCollectResponse;
            }).filter(Objects::nonNull).collect(Collectors.toList());
            return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
        }
    }

    /**
     * 新增商品收藏
     * @param commodityCollectRequest 商品收藏实体
     * @return 商品收藏id
     */
    @RequestMapping(value = "/saveCommodityCollect", method = RequestMethod.POST)
    public Wrapper<Long> saveCommodityCollect(@RequestBody CommodityCollectRequest commodityCollectRequest){
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityCollectService.saveCommodityCollect(sysUser, shopId, commodityCollectRequest));
    }

    /**
     * 批量新增商品收藏
     * @param simpleCommodityIdListRequest 商品收藏实体
     * @return
     */
    @RequestMapping(value = "/saveCommodityCollectBatch", method = RequestMethod.POST)
    public Wrapper<Boolean> saveCommodityCollectBatch(@RequestBody CommodityCollectBatchRequest simpleCommodityIdListRequest){
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityCollectService.saveBatchCommodityCollect(sysUser, shopId, simpleCommodityIdListRequest));
    }

    /**
     * 删除商品收藏
     * @param commodityCollectRequest
     * @return
     */
    @RequestMapping(value = "/deleteCommodityCollect", method = RequestMethod.POST)
    public Wrapper<String> deleteCommodityCollect(@RequestBody CommodityCollectRequest commodityCollectRequest){
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityCollectService.deleteCommodityCollect(sysUser, shopId, commodityCollectRequest));
    }

    /**
     * 删除商品收藏通过id
     * @param simpleIdRequest 商品收藏id
     * @return
     */
    @RequestMapping(value = "/deleteCommodityCollectById", method = RequestMethod.POST)
    public Wrapper<String> deleteCommodityCollectById(@RequestBody SimpleIdRequest simpleIdRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        Long id = simpleIdRequest.getId();
        return Wrapper.success(commodityCollectService.deleteCommodityCollectById(sysUser, id));
    }

}
